package com.leoman.spinningmachine.tab1;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.leoman.helper.util.AppManager;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;

import butterknife.BindView;

/**
 * 详情
 */
public class DetailActivity extends BaseActivity {

    @BindView(R.id.web)
    WebView webView;
    @BindView(R.id.progress)
    ProgressBar progressbar;
    private String url, title;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        AppManager.getInstance().addActivity(this);
        initTitle("详情");
        initWeb();
    }

    @Override
    protected void initData() {
        loadUrl();
    }

    private void loadUrl() {
        webView.loadUrl("http://www.qq.com");
        if (!TextUtils.isEmpty(url)) {
            webView.loadDataWithBaseURL(null, url, "text/html", "utf-8", null);
        }
    }

    private void initWeb() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.setVerticalScrollBarEnabled(false);
        webView.setVerticalScrollbarOverlay(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setHorizontalScrollbarOverlay(false);

        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                super.onProgressChanged(view, progress);
                if (!isFinishing()) {
                    progressbar.setProgress(progress);
                    if (progress == 100) {
                        progressbar.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

}
